package com.web.biz.common;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.platform.web.comm.WebRootUtil;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;
import com.web.common.utils.FileUploadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;
import com.web.common.utils.freemarker.FreemarkerUtil;

@ServiceContainer
public class FreeMarkerService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(FreeMarkerService.class);

	public FreeMarkerService() throws Exception {
		super();
	}


	/**
	 * 初始化静态html文件并返回文件的相对路径
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "FreeMarkerService_genHtmlAndGetPath")
	public IResData genHtmlAndGetPath(IReqData req) throws Exception {

		log.debug("开始进入 FreeMarkerService_genHtml 方法。");
		IResData res = new SwordRes();
		
		String flag = "false";
		String type = StringUtil.parse2String(req.getAttr("type"));
		String yhm = LoginInfoUtil.getLoginUserName();
		String fileName=yhm+"_new.html";
		String filepath="pages/template/genhtml/"+type+"/"+yhm+"/";
		String htmlPath = filepath+fileName;
		String htmlRealPath=WebRootUtil.getWebRootPath()+"/"+htmlPath;
		//String path = SwordFileUtils.getSwordRootPath();
		File desPath =new File(WebRootUtil.getWebRootPath()+"/"+filepath);
		//如果文件夹不存在则创建
		if  (!desPath.exists())
		{
			desPath.mkdirs();//.mkdir();
		}
		
		//通过传递过来的type来判断是那个表单提交过来的数据
		Map dataMap = new HashMap();
		if ("yzbrandweb".equals(type)) {
			dataMap.put("yzbrandwebcontent", req.getAttr("ck"));
			if (FreemarkerUtil.genHtml("brandweb_yz.ftl", dataMap, "utf-8", htmlRealPath)) {
				flag="true";
			}
		}
		if ("ybbrandweb".equals(type)) {
			Map<String, String> map = req.getForm("form_brandweb");
			List<SwordFileItem> fjlist = BaseDomainCtrl.class.newInstance().getFormUploadList2(req, "form_brandweb", "fj");
			String fj = FileUploadUtiles.uploadFileAndGetFileXML(fjlist,FileUploadUtiles.DOMAIN_SHOPWEB);
			String webfullpath = StringUtil.parse2String(FileUploadUtiles.getFileElemenstMap(fj).get("WebFullPath"));
			dataMap.put("ybbrandwebimgpath", webfullpath);
			dataMap.put("ybbrandwebtext", map.get("ybwytext"));
			if (FreemarkerUtil.genHtml("brandweb_yb.ftl", dataMap, "utf-8", htmlRealPath)) {
				flag="true";
			}
		}
		if ("recruitweb".equals(type)) {
			dataMap.put("recruitwebcontent", req.getAttr("ck"));
			if (FreemarkerUtil.genHtml("recruitweb.ftl", dataMap, "utf-8", htmlRealPath)) {
				flag="true";
			}
		}
		if ("true".equals(flag)) {
			res.put("htmlPath", htmlPath);
			res.addAttr("htmlPath", htmlPath);
		}
		res.addAttr("flag", flag);
		res.put("flag", flag);
		return res;
	}

	 		


}